<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('门店库存列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li shiro:lacksRole="StoreManager">
                                <label>门店编号：</label>
                                <input type="text" id="shopId" name="shopId"/>
                            </li>
                            <li shiro:lacksRole="StoreManager">
                                <label>门店名称：</label>
                                <input type="text" id="shopName" name="shopName"/>
                            </li>
                            <li>
                                <label>水果种类编号：</label>
                                <input type="text" id="fruittypesId" name="fruittypesId"/>
                            </li>
                            <li>
                                <label>水果种类名称：</label>
                                <input type="text" id="fruittypesName" name="fruittypesName"/>
                            </li>
                            <li>
                                <label>水果编号：</label>
                                <input type="text" id="fruitId" name="fruitId"/>
                            </li>
                            <li>
                                <label>水果名称：</label>
                                <input type="text" id="fruitName" name="fruitName"/>
                            </li>
                            <li>
                                <label>状态：</label>
                                <select name="isStatus" th:with="type=${@dict.getType('fruits')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="inventory:storeinventory:add">
                    <i class="fa fa-plus"></i> 添加门店库存
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasAnyRoles="admin, WarehouseManager" shiro:hasPermission="inventory:storeinventory:remove">
                    <i class="fa fa-remove"></i> 批量清零
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('inventory:storeinventory:edit')}]];
        var removeFlag = [[${@permission.hasPermi('inventory:storeinventory:remove')}]];
        var isStatusDatas = [[${@dict.getType('fruits')}]];
        var prefix = ctx + "inventory/storeinventory";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "门店库存",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'inentorydetailsId',
                    title: '序号',
                    formatter: function (value, row, index) {
                        return index + 1
                    }
                },
                {
                    field: 'shopId',
                    title: '门店编号',
                    visible: false
                },
                {
                    title: '门店名称',
                    formatter: function(value, row, index) {
                        (function () {
                            selectShopNameByShopId(row.shopId, index)
                        })();
                        return ('<span id=' + "shopNameSpan" + index + '></span>')
                    }
                },
                {
                    field: 'fruittypesId',
                    title: '水果种类编号',
                    visible: false
                },
                {
                    title: '水果种类名称',
                    formatter: function(value, row, index) {
                        (function () {
                            selectFruitTypeNameByFruitTypeId(row.fruittypesId, index)
                        })();
                        return ('<span id=' + "fruitTypeNameSpan" + index + '></span>')
                    }
                },
                {
                    field: 'fruitId',
                    title: '水果编号',
                    visible: false
                },
                {
                    title: '水果名称',
                    formatter: function(value, row, index) {
                        (function () {
                            selectFruitNameByFruitId(row.fruitId, index)
                        })();
                        return ('<span id=' + "fruitNameSpan" + index + '></span>')
                    }
                },
                {
                    field: 'number',
                    title: '库存(kg)'
                },
                {
                    field: 'createdBy',
                    title: '创建人'
                },
                {
                    field: 'createdTime',
                    title: '创建时间'
                },
                {
                    field: 'updatedBy',
                    title: '更新人'
                },
                {
                    field: 'updatedTime',
                    title: '更新时间'
                },
                {
                    field: 'isStatus',
                    title: '状态',
                    formatter: function(value, row, index) {
                        if (value === 1) {
                            return ('<i class="fa fa-toggle-on text-info fa-2x" onclick="storeinventoryBan(' + row.inentorydetailsId + ',' + 0 + ')"></i>')
                        } else {
                            return ('<i class="fa fa-toggle-off text-info fa-2x" onclick="storeinventoryBan(' + row.inentorydetailsId + ',' + 1 + ')"></i>')
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.inentorydetailsId + '\')"><i class="fa fa-edit"></i>修改库存</a> ');
                        var role = [[${@permission.hasAnyRoles('admin, WarehouseManager')}]]; // 验证用户是否是管理员 或者 仓库经理
                        actions.push('<a class="btn btn-danger btn-xs ' + role +  '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.inentorydetailsId + '\')"><i class="fa fa-remove"></i>清零</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });
    </script>

     <!--  根据门店名称查询门店编号  -->
     <script>
         // 监测门店名称输入框
         $(document).ready(function () {
             $("#shopName").on('input', function () {
                 var shopName = $(this).val();
                 if (shopName) {
                     selectShopIdByShopName(shopName);
                 } else {
                     $("#shopId").val(""); // 如果输入框为空就清空门店编号输入框
                 }
             })
         })

         function selectShopIdByShopName(shopName) {
             $.ajax({
                 url: "/inventory/purchaseorderdetails/selectShopId",
                 dataType: "json",
                 data: {
                     "sName": shopName
                 },
                 type: "post",
                 success: function (resp) {
                     if (resp.shopId != null && resp.shopId !== "") {
                         $("#shopId").val(resp.shopId)
                     } else {
                         $("#shopId").val("") // 如果查不到就清空门店编号输入框
                     }
                 },
                 error:function(xhr,type,errorThrown){
                     console.log('XMLHttpRequest 对象:',xhr);
                     console.log('错误类型:', type);
                     console.log('错误描述:', errorThrown);
                     $.modal.alertError("请求失败");
                 }
             })
         }

     </script>

<!--  根据门店编号查询门店名称  -->
     <script>
         // 监测门店编号输入框
         $(document).ready(function () {
             $("#shopId").on('input', function () {
                 var shopId = $(this).val();
                 if (shopId) {
                     selectShopNameByShopId(shopId);
                 } else {
                     $("#shopName").val(""); // 如果输入框为空就清空门店名称输入框
                 }
             })
         })
         // 根据门店编号查询门店名称
         function selectShopNameByShopId(shopId, index) {
             $.ajax({
                 url: "/inventory/storeinventory/selectShopName",
                 dataType: "json",
                 data: {
                     "sNumber": shopId
                 },
                 type: "post",
                 success: function (resp) {
                     if (index != null) {
                         $("#shopNameSpan" + index).html(resp.shopName)
                     } else {
                         if (resp.shopName != null && resp.shopName !== "") {
                             $("#shopName").val(resp.shopName)
                         } else {
                             $("#shopName").val("") // 如果查不到就清空门店名称输入框
                         }
                     }
                 },
                 error:function(xhr,type,errorThrown){
                     console.log('XMLHttpRequest 对象:',xhr);
                     console.log('错误类型:', type);
                     console.log('错误描述:', errorThrown);
                     $.modal.alertError("请求失败");
                 }
             })
         }
     </script>

     <!--  根据水果种类名称查询水果种类编号  -->
     <script>
         $(document).ready(function () {
             $("#fruittypesName").on('input', function () {
                 var fruittypesName = $(this).val();
                 if (fruittypesName) {
                     selectFruitTypeIdByFruitTypeName(fruittypesName);
                 } else {
                     $("#fruittypesId").val(""); // 如果输入框为空就清空水果种类编号输入框
                 }
             })
         })
         function selectFruitTypeIdByFruitTypeName(fruittypesName) {
             $.ajax({
                 url: "/inventory/fruits/selectFruitTypeId",
                 dataType: "json",
                 data: {
                     "name": fruittypesName
                 },
                 type: "post",
                 success: function (resp) {
                     if (resp.fruitTypeId != null && resp.fruitTypeId !== "") {
                         $("#fruittypesId").val(resp.fruitTypeId);
                     } else {
                         $("#fruittypesId").val(""); // 如果查不到就清空水果种类编号输入框
                     }
                 },
                 error:function(xhr,type,errorThrown){
                     console.log('XMLHttpRequest 对象:',xhr);
                     console.log('错误类型:', type);
                     console.log('错误描述:', errorThrown);
                     $.modal.alertError("请求失败");
                 }
             })
         }
     </script>

     <!--  根据水果种类编号查询水果种类名称  -->
    <script>
        // 监测水果种类id输入框
        $(document).ready(function () {
            $("#fruittypesId").on('input', function () {
                var fruittypesId = $(this).val();
                if (fruittypesId) {
                    selectFruitTypeNameByFruitTypeId(fruittypesId);
                } else {
                    $("#fruittypesName").val(""); // 如果输入框为空就清空水果种类名称输入框
                }
            })
        })

        // 根据水果种类编号查询水果种类名称
        function selectFruitTypeNameByFruitTypeId(fruittypesId, index) {
            $.ajax({
                url: "/inventory/storeinventory/selectFruitTypeName",
                dataType: "json",
                data: {
                    "code": fruittypesId
                },
                type: "post",
                success: function (resp) {
                    if (index != null) {
                        $("#fruitTypeNameSpan" + index).html(resp.fruitTypeName);
                    } else {
                        if (resp.fruitTypeName != null && resp.fruitTypeName !== "") {
                            $("#fruittypesName").val(resp.fruitTypeName);
                        } else {
                            $("#fruittypesName").val(""); // 如果查不到就清空水果种类名称输入框
                        }
                    }
                },
                error:function(xhr,type,errorThrown){
                    console.log('XMLHttpRequest 对象:',xhr);
                    console.log('错误类型:', type);
                    console.log('错误描述:', errorThrown);
                    $.modal.alertError("请求失败");
                }
            })
        }
    </script>

<!--  根据水果名称 查询水果编号  -->
     <script>
         // 监测水果名称输入框
         $(document).ready(function () {
             $("#fruitName").on('input', function () {
                 var fruitName = $(this).val();
                 if (fruitName) {
                     selectFruitIdByFruitName(fruitName);
                 } else {
                     $("#fruitId").val(""); // 如果输入框为空就清空水果编号输入框
                 }
             })
         })
         // 根据水果名称 查询水果编号
         function selectFruitIdByFruitName(fruitName, index) {
             $.ajax({
                 url: "/inventory/storeinventory/selectFruitIdByFruitName",
                 dataType: "json",
                 data: {
                     "name": fruitName
                 },
                 type: "post",
                 success: function (resp) {
                     if (resp.fruitId != null && resp.fruitId !== "") {
                         $("#fruitId").val(resp.fruitId);
                     } else {
                         $("#fruitId").val(""); // 如果查不到就清空水果编号输入框
                     }
                 },
                 error:function(xhr,type,errorThrown){
                     console.log('XMLHttpRequest 对象:',xhr);
                     console.log('错误类型:', type);
                     console.log('错误描述:', errorThrown);
                     $.modal.alertError("请求失败");
                 }
             })
         }
     </script>

<!--  根据水果编号 查询水果名称  -->
    <script>
        // 监测水果编号输入框
        $(document).ready(function () {
            $("#fruitId").on('input', function () {
                var fruitId = $(this).val();
                if (fruitId) {
                    selectFruitNameByFruitId(fruitId);
                } else {
                    $("#fruitName").val(""); // 如果输入框为空就清空水果名称输入框
                }
            })
        })
        // 根据水果编号查询水果名称
        function selectFruitNameByFruitId(fruitId, index) {
            $.ajax({
                url: "/inventory/storeinventory/selectFruitNameByFruitId",
                dataType: "json",
                data: {
                    "code": fruitId
                },
                type: "post",
                success: function (resp) {
                    if (index != null) {
                        $("#fruitNameSpan" + index).html(resp.fruitName)
                    } else {
                        if (resp.fruitName != null && resp.fruitName !== "") {
                            $("#fruitName").val(resp.fruitName);
                        } else {
                            $("#fruitName").val(""); // 如果查不到就清空水果名称输入框
                        }
                    }
                },
                error:function(xhr,type,errorThrown){
                    console.log('XMLHttpRequest 对象:',xhr);
                    console.log('错误类型:', type);
                    console.log('错误描述:', errorThrown);
                    $.modal.alertError("请求失败");
                }
            })
        }
    </script>

<!--  下架水果  -->
    <script>
        // 下架水果
        function storeinventoryBan(inentorydetailsId, isStatus) {
            let msgConfirm;
            if (isStatus === 0) {
                msgConfirm = "确定要下架该门店的该种水果吗?";
            } else {
                msgConfirm = "确定要上架该门店的该种水果吗?"
            }
            $.modal.confirm(msgConfirm, function() {
                $.modal.loading("正在处理中，请稍后...");
                setTimeout(function() {
                    storeinventoryBans(inentorydetailsId, isStatus);
                }, 1000); // 3000毫秒等于3秒
            });
        }
        function storeinventoryBans(inentorydetailsId, isStatus) {
            $.ajax({
                url: "/inventory/storeinventory/edit",
                data: {
                    "inentorydetailsId": inentorydetailsId,
                    "isStatus": isStatus
                },
                dataType: "json",
                type: "post",
                success: function (resp) {
                    $.modal.closeLoading(); // 关闭遮罩层
                    $.modal.msgSuccess(resp.msg);
                    location.reload();
                },
                error:function(xhr,type,errorThrown){
                    console.log('XMLHttpRequest 对象:',xhr);
                    console.log('错误类型:', type);
                    console.log('错误描述:', errorThrown);
                    $.modal.alertError("请求失败");
                }
            })
        }
    </script>
</body>
</html>